import board
import digitalio
import time
from fonts import *
from dcp import *
from sw import *
from oled import *

# DAC
DA0 = digitalio.DigitalInOut(board.GP28)
DA1 = digitalio.DigitalInOut(board.GP29)
DA2 = digitalio.DigitalInOut(board.GP30)
DA3 = digitalio.DigitalInOut(board.GP31)
DA4 = digitalio.DigitalInOut(board.GP32)
DA5 = digitalio.DigitalInOut(board.GP33)
DA6 = digitalio.DigitalInOut(board.GP34)
DA7 = digitalio.DigitalInOut(board.GP35)
DA8 = digitalio.DigitalInOut(board.GP36)
DA9 = digitalio.DigitalInOut(board.GP37)

DA0.direction = digitalio.Direction.OUTPUT
DA1.direction = digitalio.Direction.OUTPUT
DA2.direction = digitalio.Direction.OUTPUT
DA3.direction = digitalio.Direction.OUTPUT
DA4.direction = digitalio.Direction.OUTPUT
DA5.direction = digitalio.Direction.OUTPUT
DA6.direction = digitalio.Direction.OUTPUT
DA7.direction = digitalio.Direction.OUTPUT
DA8.direction = digitalio.Direction.OUTPUT
DA9.direction = digitalio.Direction.OUTPUT

def get_DAC_data(cmd):
    
    # SINE
    if(cmd == "1"):
        data = [511, 543, 575, 607, 638, 669, 699, 729, 757, 785, 811, 837, 861, 884, 905, 924, 942, 959, 973 ,986, 997, 1006, 1013, 1018, 1021, 1022, 1021, 1018, 1013, 1006, 997, 986, 973, 959, 942, 924, 905, 884, 861, 837, 811, 785, 757, 729, 699, 669, 638, 607, 575, 543, 511, 479, 447, 415, 384, 353, 323, 293, 265, 237, 211, 185, 161, 138, 117, 98, 80, 63, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 63, 80, 98, 117, 138, 161, 185, 211, 237, 265, 293, 323, 353, 384, 415, 447, 479]
    
    # TRAINGLE
    elif(cmd == "2"):
        data = [0, 20, 41, 61, 82, 102, 123, 143, 164, 184, 205, 225, 246, 266, 286, 307, 327, 348, 368, 389, 409, 430, 450, 471, 491, 512, 532, 552, 573, 593, 614, 634, 655, 675, 696, 716, 737, 757, 777, 798, 818, 839, 859, 880, 900, 921, 941, 962, 982, 1003, 1023, 1003, 982, 962, 941, 921, 900, 880, 859, 839, 818, 798, 777, 757, 737, 716, 696, 675, 655, 634, 614, 593, 573, 552, 532, 512, 491, 471, 450, 430, 409, 389, 368, 348, 327, 307, 286, 266, 246, 225, 205, 184, 164, 143, 123, 102, 82, 61, 41, 20]
        
    # SQUARE
    elif(cmd == "3"):
        data = [1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1203, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1032, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    # DC
    elif(cmd == "4"):
        data = [1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023,1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023]
    else:
        data = [0]
    return data



if __name__ == "__main__":

    # 波形显示位置
    wave_pos_x = 5
    wave_pos_y = 10
    
    # 频率定义
    freq = 100_00
    freq_top = 200_000
    freq_buttom = 100
    freq_step = 200
    
    # 电压值显示位置
    voltage_pos_x = 80
    voltage_pos_y = 5
    
    # 频率值显示位置
    freq_pos_x = 80
    freq_pos_y = 15
    
    # 波形初始化
    cmd = "0"
    dcp = 3.3
    DAC_data = get_DAC_data(cmd)
    
    while True:
        for m in range(100):
            DAC_data = get_DAC_data(cmd)
            for i in range(len(DAC_data)):
                DAC_data_t = DAC_data[i] * dcp / 3.3
                DAC_data_t = int(DAC_data_t)
                DA0.value = bool(DAC_data_t & 0b00_0000_0001)
                DA1.value = bool(DAC_data_t & 0b00_0000_0010)
                DA2.value = bool(DAC_data_t & 0b00_0000_0100)
                DA3.value = bool(DAC_data_t & 0b00_0000_1000)
                DA4.value = bool(DAC_data_t & 0b00_0001_0000)
                DA5.value = bool(DAC_data_t & 0b00_0010_0000)
                DA6.value = bool(DAC_data_t & 0b00_0100_0000)
                DA7.value = bool(DAC_data_t & 0b00_1000_0000)
                DA8.value = bool(DAC_data_t & 0b01_0000_0000)
                DA9.value = bool(DAC_data_t & 0b10_0000_0000)


                if (i == 0 and m%2 == 0):
                    # 读数据
                    dcp = read_dcp()
                    adc_res = []
                    for i in range(10):
                        adc_res.append(ADC.value)
                    ADC_value = sum(adc_res)/len(adc_res)*3.3/65535
                    adc = choose_sw(ADC_value)
                    # 拨码开关修改波形
                    if ((adc>0) and (adc<=4)):
                        cmd = str(adc)
                        wave = str(adc)
                        wave_font = wave_fonts[str(wave)]
                        oled.fill_rect(wave_pos_x, wave_pos_y, wave_pos_x+40, wave_pos_y+8, 0)
                        for i,j in wave_font:
                            oled.pixel(i+wave_pos_x, j+wave_pos_y, 1)
                        
                    # 最左按键使频率++
                    elif (adc==5):
                        if (freq <= freq_top-freq_step):
                            freq += freq_step
                            print("freq++:",freq)
                        else:
                            print("到频率上限")
                                
                    # 最右按键使频率--
                    elif (adc==6):
                        if (freq >= freq_buttom+freq_step):
                            freq -= freq_step
                            print("freq--:",freq)
                        else:
                            print("到频率下限")
                                
                    # 修改频率步长
                    if (freq<1000):
                        freq_step = 100
                    elif (freq<1e5):
                        freq_step = 500
                    else:
                        freq_stop = 2000
                elif (i == 1 and m == 0):        
                    # 电压显示
                    voltage_show = str(round(dcp,2))
                    voltage_show = "2A="+voltage_show+"V"
                    oled.fill_rect(voltage_pos_x, voltage_pos_y, voltage_pos_x+5*8, voltage_pos_y+8, 0)
                    for x in range(len(voltage_show)):
                        voltage_show_char = voltage_show[x]
                        voltage_show_font = num_fonts[voltage_show_char]
                        for i,j in voltage_show_font:
                            oled.pixel(j+voltage_pos_x+int(5*x), i+voltage_pos_y, 1)
                                
                    # 频率显示
                    freq_show = str(freq)
                    oled.fill_rect(freq_pos_x, freq_pos_y, freq_pos_x+5*8, freq_pos_y+8, 0)
                    if(len(freq_show)<=3):
                        freq_show = "F="+freq_show+"Hz"
                    else:
                        freq_show = "F="+str(round(freq/1000))+"kHz"
                    for x in range(len(freq_show)):
                        freq_show_char = freq_show[x]
                        freq_show_font = num_fonts[freq_show_char]
                        for i,j in freq_show_font:
                            oled.pixel(j+freq_pos_x+int(5*x), i+freq_pos_y, 1)
                    oled.show()
                time.sleep(1/freq/len(DAC_data))

